package com.szp.drug.component.controller;

import com.szp.drug.entity.DrugRole;
import com.szp.drug.component.service.api.DrugAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 权限分配前端控制器
 * @author SuZePing
 * @create 2021-02-02 16:19
 * @E-mail suzeping10@126.com
 */
@Slf4j
@Controller
@RequestMapping("/assign")
public class DrugAssignController {

	@Autowired
	private DrugAdminService adminService;

	/**
	 * 跳转到为管理员分配角色的页面
	 * @param adminId
	 * @param map
	 * @return
	 */
	@GetMapping("/to/adminRole/page.html")
	public String toAdminRolePage(@RequestParam("adminId")String adminId, ModelMap map){
		// 查询该管理员已拥有的角色
		List<DrugRole> assignedRoles = adminService.getAssignedRole(adminId);
		// 查询该管理员未拥有的角色
		List<DrugRole> unassignedRoles = adminService.getUnassignedRole(adminId);
		map.addAttribute("assignedRoles",assignedRoles);
		map.addAttribute("unassignedRoles",unassignedRoles);
		return "assign-admin-role";
	}

	/**
	 * 为管理员分配角色
	 * @param adminId
	 * @param pageNum
	 * @param keyword
	 * @param roleIdList 不是必须需要的
	 * @return
	 */
	@PostMapping("/do/adminRole.html")
	public String saveAdminToRole(
			@RequestParam("adminId")String adminId,
			@RequestParam("pageNum")String pageNum,
			@RequestParam("keyword")String keyword,
			@RequestParam(value = "assignRoleIdList",required = false) List<String> roleIdList
			){
		adminService.saveAssignAdminRole(adminId,roleIdList);
		return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
	}
}